package com.dd.cloud.common.single;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import toolgood.words.StringSearch;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: 屏蔽词单例类
 * @Author: czg
 * @Date: 2023/11/20 17:50
 * @Version: 1.0.0
 */
@Slf4j
public class BlockKeywordsSingle {
    private List<String> keywordList = null;
    private StringSearch stringSearch = null;
    private static BlockKeywordsSingle singleton = null;

    /**
     * 使用private修改默认构造方法，禁止使用new关键字创建类的实例对象, 实例化对象属性
     */
    private BlockKeywordsSingle() {

    }

    /**
     *
     * 获取对象
     * @return {@link BlockKeywordsSingle}
     */
    private static BlockKeywordsSingle getInstance() {

        if (singleton != null){
            return singleton;
        }
        singleton = new BlockKeywordsSingle();
        try {
            ClassPathResource resource = new ClassPathResource("template/sensi_words.txt");
            InputStreamReader inputStream = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
            StringBuilder sb = new StringBuilder();
            try {
                char[] chars = new char[1024];
                int len;
                while ((len = inputStream.read(chars)) != -1) {
                    sb.append(chars, 0, len);
                }
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            String[] str = sb.toString().split("\n");
            singleton.keywordList = new ArrayList<>(str.length);
            Collections.addAll(singleton.keywordList, str);
            StringSearch search = new StringSearch();
            search.SetKeywords(singleton.keywordList);
            singleton.stringSearch = search;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return singleton;
    }

    public static boolean hasBlockKeyword(String str) {
        StringSearch search = BlockKeywordsSingle.getInstance().stringSearch;
        List<String> strings = search.FindAll(str);
        log.error("含有屏蔽关键字, {}", strings.toString());
        boolean has = search.ContainsAny(str);
        return has;
    }

    public static void main(String[] args) {
        String str = "{isAiPres=0, isPay=1, medId=000012, depName=内科, pharId=24, symptomList=咳嗽,头痛, disease=感冒, dayDose=1, drugDose=100, allDose=2, remark=无, advice=测试, treatAdvice=测试, drugUsage=0, doseNum=1, packagenum=180, consultationFee=0, expensesFee=0, fryingFee=400, serviceFee=0, money=260, discountMoney=282, payType=WX_NATIVE, sign=https://zk-med-test.obs.cn-south-1.myhuaweicloud.com/dadao/QICsKq9lXHqya63a40411f34de558b8c0b719d12df80.png, presType=1, deliveryIsDisplay=1, sourceType=1, recvName=测试, recvTel=15923910222, provinceName=广州省, cityName=深圳市, areaName=宝安区, address=汇聚409, medicationMethod=1, tcms=[{herbName=三七, drugnum=HST0000000012, specification=一袋50g, drugdescription=, drugallnum=5, unit=g, retailprice=13, discountPrice=13, decocting=xianjian}, {herbName=丁香, drugnum=HST0000000003, specification=一袋10g, drugdescription=, drugallnum=3, unit=g, retailprice=13, discountPrice=13, decocting=houxia}, {herbName=百合, drugnum=HST0000000204, specification=一袋15g, drugdescription=, drugallnum=2, unit=g, retailprice=13, discountPrice=13, decocting=houxia}]}";
        boolean hasBlockKeyword = BlockKeywordsSingle.hasBlockKeyword(str);

    }
}
